var logManager=require("./Logs.js");
var DelayExeData=Class.extend({
    thisObj:null,
    exeTime:0,
    fun:null,
    args:null,
    id:0,
    ctor:function(id, et, fun, args,thisObj)
    {
        this.id = id;
        this.exeTime = et;
        this.fun = fun;
        this.args = args;
        this.thisObj=thisObj;
    },
    exe:function(t)
    {
        if (this.fun == null)
            return (true);
        if (t > this.exeTime)
            this.fun.apply(this.thisObj?this.thisObj:null, this.args);
        else
            return (false);
        return (true);
    },
    clone:function() {
        return (new DelayExeData(this.id, this.exeTime, this.fun, this.args,this.thisObj));
    },
    dispose:function()
    {
        this.fun = null;
        this.args = null;
    }
});
//延迟调用，定时器处理
var TimerManager=Class.extend({
    _taskId:1,
    _destructed:false,
    _currentDelayExeData:[],
    _nextDelayExeData:[],
    _countedRealTime:0,
    _frameListener:[],/*{fun,thisObj,param}*/
    _interval:null,
    init:function(){
        // cc.director.getRunningScene().schedule(this.onTimer.bind(this),0.033);
        this._interval=setInterval(this.onTimer.bind(this),100);
        this._countedRealTime=new Date().getTime();
    },
    destruct:function(){
        clearInterval(this._interval);
    },
    getCurrentTime:function(){
        return this._countedRealTime;
    },
    onTimer:function(){
        this._countedRealTime=new Date().getTime();
        this.delayExe();

        for(var i=this._frameListener.length-1;i>=0;i--){
            var tem=this._frameListener[i];
            tem.fun.apply(tem.thisObj,tem.param);
        }
    },
    addFrameListener:function(fun,thisObj,param){
        for(var i=0;i<this._frameListener;i++){
            var tem=this._frameListener[i];
            if(tem&&tem.fun==fun&&tem.thisObj==thisObj)
            {
                logManager.logW("aready has the same frame listener");
                return;
            }
        }
        this._frameListener.push({fun:fun,thisObj:thisObj,param:param});
    },
    removeFrameListener:function(fun,thisObj){
        for(var i=this._frameListener.length-1;i>=0;i--){
            var tem=this._frameListener[i];
            if(tem&&tem.fun==fun&&tem.thisObj==thisObj)
            {
                this._frameListener.splice(i,1);
            }
        }
    },
    addTask:function(fun, param, date,thisObj)
    {
        this._taskId++;
        var task=null;
        if (date)
            task = new DelayExeData(this._taskId, date.getTime(), fun, param,thisObj);
        else
            task = new DelayExeData(this._taskId, 0, fun, param,thisObj);
        this._nextDelayExeData.push(task);
        return (task.id);
    },
    /**
     * 添加延迟调用
     * @param fun
     * @param time(毫秒计)
     * @param args
     */
    addDelayCall:function(fun, time, args,thisObj)
    {
        if(!time)
            time=0;
        this._taskId++;
        var task = new DelayExeData(this._taskId, (this._countedRealTime + time), fun, args,thisObj);
        this._nextDelayExeData.push(task);
        // logManager.logM("add task:"+task.id+",at:"+task.exeTime)
        return (task.id);
    },
    delTask:function(id)
    {
        var task;
        var i=0;
        while (i < this._currentDelayExeData.length)
        {
            task = this._currentDelayExeData[i];
            if (task.id == id)
            {
                logManager.logM("del task:"+id+"-sucess");
                this._currentDelayExeData.splice(i, 1);
                return;
            };
            i++;
        };
        var j=0;
        while (j < this._nextDelayExeData.length)
        {
            task = this._nextDelayExeData[j];
            if (task.id == id)
            {
                logManager.logM("del task:"+id+"-sucess");
                this._nextDelayExeData.splice(j, 1);
                return;
            };
            j++;
        };
        logManager.logM("del task:"+id+"-faild");
    },
    delayExe:function()
    {
        var task;
        var obj;
        var k=0;
        var task2;
        var j=0;
        while (this._currentDelayExeData.length)
        {
            obj = this._currentDelayExeData.shift();
            if (((!(obj)) || (obj.exe(this._countedRealTime))))
            {
            } else
            {
                this._currentDelayExeData.unshift(obj);
                break;
            };
        };
        var find;
        if (((this._nextDelayExeData) && ((this._nextDelayExeData.length > 0))))
        {
            k = 0;
            while (k < this._nextDelayExeData.length)
            {
                find = false;
                task2 = this._nextDelayExeData[k];
                j = 0;
                while (j < this._currentDelayExeData.length)
                {
                    task = this._currentDelayExeData[j];
                    if (task2.exeTime < task.exeTime)
                    {
                        this._currentDelayExeData.splice(j, 0, task2);
                        find = true;
                        break;
                    };
                    j++;
                };
                if (!(find))
                {
                    this._currentDelayExeData.push(task2);
                };
                k++;
            };
            this._nextDelayExeData.splice(0, this._nextDelayExeData.length);
        };
        if (this._destructed)
        {
            this._currentDelayExeData = null;
            this._nextDelayExeData = null;
        };
    }
});

module.exports=TimerManager;